﻿using AntDesign.Core.Extensions;
using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EmployeeLeaveSystem.Server.Controllers.MoveController {
    [Route("api/Move/[action]")]
    [ApiController]
    public class MainController : ControllerBase {
        readonly EpMainContext _context;
        public MainController(EpMainContext context) { _context = context; }
        // 人事调动控制器
        [HttpPost]
        public ActionResult AddMoveInfo(AddMoveDepartment addMoveDepartment) {
            addMoveDepartment.Created = DateTime.Now;
            _context.AddMoves.Add(addMoveDepartment);
            _context.SaveChanges();
            return Ok();
        }
        // 退休申请控制器
        [HttpPost]
        public ActionResult AddRetireMove(AddRetireMove addRetireMove) {
            addRetireMove.Created = DateTime.Now;
            _context.AddRetireMoves.Add(addRetireMove);
            _context.SaveChanges();
            return Ok();
        }
        // 获取所有的人事调动记录
        [HttpGet]
        public ActionResult<List<AddMoveDepartment>> GetMoveInfos(int usrId) {
            return _context.AddMoves.Include(x => x.ToDepartment).Where(x => x.StaffId.Equals(usrId)).ToList();
        }
        // 获取所有的退休调动记录
        [HttpGet]
        public ActionResult<List<AddRetireMove>> GetRetireInfos(int usrId) {
            return _context.AddRetireMoves.Where(x => x.StaffId.Equals(usrId)).ToList();
        }
        // 查找特定ID的调动历史记录 退休记录
        public ActionResult<AddRetireMove> GetInfoRetireById(int id) {
            return _context.AddRetireMoves.Where(x => x.Id.Equals(id)).Include(x => x.Staff).Include(x => x.Files).First();
        }
        // 查找特定ID的调动历史记录 调动记录
        public ActionResult<AddMoveDepartment> GetInfoMoveById(int id) {
            return _context.AddMoves.Where(x => x.Id.Equals(id)).Include(x => x.Staff).Include(x => x.Files).Include(x => x.ToDepartment).First();
        }
        /// <summary>
        /// 筛选出所有的在历史记录名单里面的，需要上司审核的内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult<List<AddMoveDepartment>> GetAddMovesManagerUsrId(int id) {
            // 获得自己要管理的人员条件
            var myPeoples = _context.StaffInfos.Where(x => x.UpperStaff.Id == id).Select(x => x.Id).ToList();
            return _context.AddMoves.Where(x => myPeoples.Contains((int)x.StaffId)).Include(x => x.Staff).Include(x => x.ToDepartment).ToList();
        }
        /// <summary>
        /// 更新AddMove的状态
        /// </summary>
        [HttpGet, HttpPost]
        public ActionResult UpdateMove(AcceptFlag acceptFlag, int id) {
            var enty = _context.AddMoves.Where(x => x.Id.Equals(id)).FirstOrDefault();
            enty.AcceptFlag = acceptFlag;
            if (acceptFlag == AcceptFlag.接受) {
                var usr = _context.StaffInfos.Where(x => x.Id.Equals(enty.StaffId)).FirstOrDefault();
                usr.DepartmentId = enty.ToDepartmentId;
                _context.StaffInfos.Update(usr);
            }
            _context.AddMoves.Update(enty);
            _context.SaveChanges();
            return Ok();
        }
        /// <summary>
        /// 筛选出所有符合条件的退休记录着
        /// </summary>
        public ActionResult<List<AddRetireMove>> GetAddMovesRetireUsrId(int id) {
            // 获得自己要管理的人员条件
            var myPeoples = _context.StaffInfos.Where(x => x.UpperStaff.Id == id).Select(x => x.Id).ToList();
            return _context.AddRetireMoves.Where(x => myPeoples.Contains((int)x.StaffId)).Include(x => x.Staff).ToList();
        }
        /// <summary>
        /// 更新退休记录
        /// </summary>
        [HttpGet, HttpPost]
        public ActionResult UpdateRetire(AcceptFlag acceptFlag, int id) {
            var enty = _context.AddRetireMoves.Where(x => x.Id.Equals(id)).FirstOrDefault();
            enty.AcceptFlag = acceptFlag;
            if (acceptFlag == AcceptFlag.接受) {
                var usr = _context.StaffInfos.Where(x => x.Id.Equals(enty.StaffId)).FirstOrDefault();
                usr.StaffState = StaffState.Retire;
                _context.StaffInfos.Update(usr);
            }
            _context.AddRetireMoves.Update(enty);
            _context.SaveChanges();
            return Ok();
        }
    }
}
